package com.tao.controller;


import com.alibaba.fastjson.JSONObject;
import com.tao.pojo.Answerer;
import com.tao.pojo.Miaosha;
import com.tao.service.AnswererService;
import com.tao.service.MiaoshaService;
import com.tao.service.SubmittableService;
import com.tao.service.impl.Mq.MiaoshaSendService;
import io.lettuce.core.dynamic.annotation.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author: alubus
 * @Date: 2021/11/11 17:04
 * @Package: com.tao.controller
 */
@RestController
@RequestMapping("/miaosha")
public class MiaoshaController {
    Logger logger = LoggerFactory.getLogger(getClass());


    @Autowired
    MiaoshaService miaoshaServiceImpl;

    @Autowired
    MiaoshaSendService miaoshaSendService;




    // 发送秒杀请求 减一库存  需要 qid  aid  localhost:8001/miaosha/reduceOne/9527/8888
    @RequestMapping("/reduceOne/{qid}/{aid}")
    public String reduceone4(@PathVariable("qid") Long qid,@PathVariable("aid") Long aid){
        String s = miaoshaSendService.SubmitMiaosha(qid, aid);
        return s;
    }

    //    重置 id 的秒杀数据 为  sum       localhost:8001/miaosha/setSum/8888/999
    @RequestMapping("/setSum/{aid}/{sum}")
    public String resetMiaosha2(@PathVariable("aid") Long aid,@PathVariable("sum") int sum){
//        id 就是aid
        miaoshaServiceImpl.setMiashaSum(aid,sum);
        return "success";
    }



    //通过秒杀 Id 查询商品
//    /miaosha/selectA/{666}
    @RequestMapping("/selectA/{id}")
    public Answerer selectAnswerer(@PathVariable("id") Long id){
//        通过 miaosha id查询商品
        Answerer answerer = miaoshaServiceImpl.selectAnswererByMid(id);
        return answerer;
    }

    /**
     *
     *getMiaoshatables 查看秒杀数据
     * @return
     */
    @RequestMapping("/getMiaoshatables")
    public JSONObject selectAnswerer(){
        JSONObject oj2 = new JSONObject();
        JSONObject oj = new JSONObject();

        List<Long> miaoshaIds = miaoshaServiceImpl.getMiaoshaIds();
        if (miaoshaIds.size()==0){
            oj.put("msg","error");
            return oj;
        }
        Map<String, Integer> miaoshaValues = miaoshaServiceImpl.getMiaoshaValues();
        oj2.putAll(miaoshaValues);
        oj.put("miaoshaCount",oj2);

        List<Answerer> answerersByKeys = miaoshaServiceImpl.getAnswerersByKeys();
        oj.put("answerers",answerersByKeys);


        return  oj;

    }


}
